<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Content
 *
 * @author dung
 */
class Content extends Eloquent
{
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    //put your code here

    public function author()
    {
        return $this->belongsTo('User');
    }

    public function tone()
    {
        return $this->belongsTo('Category');
    }


    public function saveItem($arrParams, $options = null)
    {
//        $row = $this->firstOrNew(array('id' => $arrParams['id']));
//        foreach ($arrParams as $key => $value) {
//            if ($key != 'id')
//                $row->{$key} = $value;
//        }
//        $row->save();
//        return $row->id;
    }


    public function listItem($arrParams, $opions = null)
    {
        $content = $this->where('type', '=', $arrParams['type']);
        if (isset($arrParams['conditions'])) {
            foreach ($arrParams['conditions'] as $condition) {
                $content = $content->where($condition['field'], $condition['opr'], $condition['value']);
            }
        }
        if (isset($arrParams['joins'])) {
            foreach ($arrParams['joins'] as $join) {
                $content = $content->join($join['table'], $join['primary'], $join['opr'], $join['foreign']);
            }
        }
        if (isset($arrParams['left_joins'])) {
            foreach ($arrParams['left_joins'] as $join) {
                $content = $content->leftJoin($join['table'], $join['primary'], $join['opr'], $join['foreign']);
            }
        }
        if (isset($arrParams['orders'])) {
            $content = $content->orderBy($arrParams['orders']['field'], $arrParams['orders']['type']);
        }
        if (isset($arrParams['take'])) {
            $content = $content->take($arrParams['take']);
        }
        if (isset($arrParams['selects'])) {
            $content = $content->select($arrParams['selects']);
        }
        if (isset($arrParams['paginate'])) {
            $content = $content->paginate($arrParams['paginate']['nums']);
            if (isset($arrParams['paginate']['appends']))
                $content = $content->appends($arrParams['paginate']['appends']);
        } else {
            $content = $content->get();
        }
        return $content;

    }

    public function selectById($arrParams, $options = null)
    {
        $row = $this->where('old', '=', $arrParams['id'])->where('type', '=', $arrParams['type'])->first();
        if (is_null($row))
            $row = $this->find($arrParams['id']);
        if (!is_null($row) && $row->type == $arrParams['type'])
            return $row;
        else
            return null;
    }


    public function getTopLyrics($type)
    {
        $key = 'lyrics-' . $type;
        $lyrics = array();
        if (Cache::has($key)) {
            $lyrics = Cache::get($key);
        } else {
            $arrParams = array(
                'type' => 'lyric',
                'conditions' => array(
                    array('field' => 'status', 'opr' => '=', 'value' => 'published')
                ),
                'selects' => array('id', 'old', 'title'),
                'orders' => array('field' => $type, 'type' => 'DESC')
            );
            $lyrics = $this->listItem($arrParams);
            Cache::put($key, $lyrics, 30);
        }
        return $lyrics;
    }

}
